package de.bht.netviz.useradm.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;

/**
 * Repräsentation eines registrierten Benutzers
 * @author Marius Hentschel
 * @since  2012/11/05
 */
@Entity
@Table( name="user", schema="app" )
public class RegisteredUser implements Serializable {
   
   private long id;
   private String firstname;
   private String lastname;
   private String username;
   private String email;
   private String passHash;
   private Date   lastLogin;
   private Date   lastChange;
   private Date   signupDate;
   
   /**
    * Standardkonstruktor initialisert mit NULL-Werten
    */
   public RegisteredUser() {
      id = 0;
      firstname  = null;
      lastname   = null;
      username   = null;
      email      = null;
      passHash   = null;
      lastLogin  = null;
      lastChange = null;
      signupDate = null;
   }

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }

   public String getFirstname() {
      return firstname;
   }

   public void setFirstname(String firstname) {
      this.firstname = firstname;
   }

   public String getLastname() {
      return lastname;
   }

   public void setLastname(String lastname) {
      this.lastname = lastname;
   }

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getEmail() {
      return this.email;
   }
   
   public void setEmail( String email ) {
      this.email = email;
   }
   public String getPassHash() {
      return passHash;
   }

   public void setPassHash(String passHash) {
      this.passHash = passHash;
   }

   @Temporal(javax.persistence.TemporalType.TIMESTAMP)
   public Date getLastLogin() {
      return lastLogin;
   }

   public void setLastLogin(Date lastLogin) {
      this.lastLogin = lastLogin;
   }

   @Temporal(javax.persistence.TemporalType.TIMESTAMP)
   public Date getLastChange() {
      return lastChange;
   }

   public void setLastChange(Date lastChange) {
      this.lastChange = lastChange;
   }

   @Temporal(javax.persistence.TemporalType.DATE)
   public Date getSignupDate() {
      return signupDate;
   }

   public void setSignupDate(Date signupDate) {
      this.signupDate = signupDate;
   }
   
   

   @Override
   public int hashCode() {
      int hash = 7;
      hash = 83 * hash + (int) (this.id ^ (this.id >>> 32));
      hash = 83 * hash + Objects.hashCode(this.firstname);
      hash = 83 * hash + Objects.hashCode(this.lastname);
      hash = 83 * hash + Objects.hashCode(this.username);
      hash = 83 * hash + Objects.hashCode(this.email);
      hash = 83 * hash + Objects.hashCode(this.passHash);
      hash = 83 * hash + Objects.hashCode(this.lastLogin);
      hash = 83 * hash + Objects.hashCode(this.lastChange);
      hash = 83 * hash + Objects.hashCode(this.signupDate);
      return hash;
   }

   @Override
   public boolean equals(Object obj) {
      if (obj == null) {
         return false;
      }
      if (getClass() != obj.getClass()) {
         return false;
      }
      final RegisteredUser other = (RegisteredUser) obj;
      if (this.id != other.id) {
         return false;
      }
      if (!Objects.equals(this.firstname, other.firstname)) {
         return false;
      }
      if (!Objects.equals(this.lastname, other.lastname)) {
         return false;
      }
      if (!Objects.equals(this.username, other.username)) {
         return false;
      }
      if (!Objects.equals(this.email, other.email)) {
         return false;
      }
      if (!Objects.equals(this.passHash, other.passHash)) {
         return false;
      }
      if (!Objects.equals(this.lastLogin, other.lastLogin)) {
         return false;
      }
      if (!Objects.equals(this.lastChange, other.lastChange)) {
         return false;
      }
      if (!Objects.equals(this.signupDate, other.signupDate)) {
         return false;
      }
      return true;
   }
   
   public String toString() {
      SimpleDateFormat f = new SimpleDateFormat();
      return "RegisteredUser #" + this.id + " [ username = " + this.username + 
             ", firstname = " + this.firstname +", lastname = " + this.lastname +
             ", email = " + this.email + ", passwordHash = " + this.passHash +
             ", signupDate = " + f.format(this.signupDate) + ", lastChange = " +
              f.format( this.lastChange ) + ", lastLogin = " + f.format( this.lastLogin ) + " ]";
   }
}
